package com.sfx.concurrent.demo.designMode.template;

import java.util.Collections;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-11-05
 * Time: 16:53
 */
public class TemplateDemo {

    static abstract class AbstractAction {
        // 定义模版方法
        public void template() {
            System.out.println("开始执行模版方法模式");
            beforeAction();
            action();
            afterAction();
        }

        // 具体方法
        public void beforeAction() {
            System.out.println("action执行前");
        }

        // 钩子方法
        public abstract void action();

        // 具体方法
        public void afterAction() {
            System.out.println("action执行后");
        }
    }

    static class ActionA extends AbstractAction{
        @Override
        public void action() {
            System.out.println("ActionA 执行");
        }
    }

    static class ActionB extends AbstractAction{
        @Override
        public void action() {
            System.out.println("ActionB 执行");
        }
    }

    public static void main(String[] args) {

        ReentrantLock lock = new ReentrantLock();
        lock.lock();
        AbstractAction action = null;
        action = new ActionA();
        action.template();
        System.out.println("==================================================");
        action = new ActionB();
        action.template();
    }
}